package com.shenmazong.rand;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @program: server-java-demo
 * @description: ListRandom
 * @author: 亮子说编程
 * @create: 2020-10-16 10:08
 **/
public class ListRandom {
    public static void main(String[] args) {

        //-- 创建list
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < 1000000; i++) {
            list.add("list"+i);
        }

        //-- 随机访问
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < list.size(); i++) {
            String msg = list.get(i);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("随机访问时间："+(endTime-startTime));

        //-- 顺序访问
        startTime = System.currentTimeMillis();
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String msg = iterator.next();
        }
        endTime = System.currentTimeMillis();
        System.out.println("顺序访问时间："+(endTime-startTime));
    }
}
